﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLKetNoi;
using QLPhanQuyen.BO;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.ViewInfo;
using DevExpress.XtraTreeList.Nodes.Operations;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Columns;

namespace QLPhanQuyen.GUI
{
    public partial class ThemVaiTroForm : Office2007Form
    {
        NhomChucNangBO NCNBO = new NhomChucNangBO();
        ChucNangBO CNBO = new ChucNangBO();
        ChucNangConBO CNCBO = new ChucNangConBO();
        VaiTroBO VTBO = new VaiTroBO();
        QuyenHanBO QHBO = new QuyenHanBO();
        VaiTro_ChucNangCon_QuyenHanBO VTCNCQHBO = new VaiTro_ChucNangCon_QuyenHanBO();
        

        public ThemVaiTroForm()
        {
            InitializeComponent();
        }

        private void ThemVaiTroForm_Load(object sender, EventArgs e)
        {
            KhoiTaoCay();
            txtMaVaiTro.Text = MaTuTang.MaTiepTheo(VTBO.MaCaoNhat(), "VT");
        }
        
        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                ThemVaiTro();
                ThemVaiTroChucNangConQuyenHan();
                MessageBox.Show("Thêm Vai Trò Thành Công");
            }
            catch(Exception)
            {
                MessageBox.Show("Thêm Không Thành Công");
            }
        }

        #region Them vai tro va them quyen han

        private bool KiemTraTextBox()
        {
            bool kiemtra = true;
            if(txtTenVaiTro.Text=="")
            {
                lblLoiTenVaiTro.Visible = true;
                kiemtra = false;
            }
            return kiemtra;
        }

        private void ThemVaiTro()
        {
            VAITRO VT = new VAITRO();
            VT.MaVaiTro = MaTuTang.MaTiepTheo(VTBO.MaCaoNhat(), "VT");
            VT.TenVaiTro = txtTenVaiTro.Text;
            VT.DienGiai = txtDienGiai.Text;
            VT.TrangThai = checkKichHoat.Checked;

            VTBO.ThemVaiTro(VT);
        }
        //private void ThemChucNangQuyenHan()
        //{
        //    for (int pn = 0; pn < tlThemVaiTro.Nodes.Count; pn++)   // rn == rootnode là node cha của cây
        //    {
        //        TreeListNode parent = tlThemVaiTro.Nodes[pn];
        //        if (DanhSachCheckCuaNode(parent) != null)
        //        {
        //            string maNhomChucNang = LayTextNode(parent);
        //            for (int i = 0; i < DanhSachCheckCuaNode(parent).Count; i++)
        //            {
        //                string maQuyenHan = DanhSachCheckCuaNode(parent)[i];
        //            }
        //        }

        //        for (int cn = 0; cn < parent.Nodes.Count; cn++)     //child node == cn node con của cha
        //        {
        //            TreeListNode child = parent.Nodes[cn];
        //            if (DanhSachCheckCuaNode(child) != null)
        //            {
        //                string maChucNang = LayTextNode(child);
        //                for (int i = 0; i < DanhSachCheckCuaNode(child).Count; i++)
        //                {
        //                    string maQuyenHan = DanhSachCheckCuaNode(child)[i];
        //                }
        //            }

        //            for (int gcn = 0; gcn < child.Nodes.Count; gcn++)    //gcn == grandchildnode node chau
        //            {
        //                TreeListNode grandchild = child.Nodes[gcn];
        //                if (DanhSachCheckCuaNode(grandchild) != null)
        //                {
        //                    string maChucNangCon = LayTextNode(grandchild);
        //                    for (int i = 0; i < DanhSachCheckCuaNode(grandchild).Count; i++)
        //                    {
        //                        string maQuyenHan = DanhSachCheckCuaNode(grandchild)[i];
        //                    }
        //                }
        //            }
        //        }
        //    }
        //}

        private void ThemVaiTroChucNangConQuyenHan(string maVT, string maCNC, string maQH)
        {
            VAITRO_CHUCNANGCON_QUYENHAN VTCNCQH = new VAITRO_CHUCNANGCON_QUYENHAN();

            VTCNCQH.MaVaiTro = maVT;
            VTCNCQH.MaChucNangCon = maCNC;
            VTCNCQH.MaQuyenHan = maQH;

            VTCNCQHBO.ThemVT_CNC_QH(VTCNCQH);
        }

        private void ThemVaiTroChucNangConQuyenHan()
        { 
            string maMaVaiTro = VTBO.MaCaoNhat(); 
            for (int pn = 0; pn < tlThemVaiTro.Nodes.Count; pn++)   // rn == rootnode là node cha của cây
            {
                TreeListNode parent = tlThemVaiTro.Nodes[pn];
                
                for (int cn = 0; cn < parent.Nodes.Count; cn++)     //child node == cn node con của cha
                {
                    TreeListNode child = parent.Nodes[cn];
                    
                    for(int gcn = 0; gcn < child.Nodes.Count; gcn++)    //gcn == grandchildnode node chau
                    {
                        TreeListNode grandchild = child.Nodes[gcn];
                        if (DanhSachCheckCuaNode(grandchild) != null)
                        {
                            string maChucNangCon = CNCBO.LayMaTheoTen(LayTextNode(grandchild));
                            for (int i = 0; i < DanhSachCheckCuaNode(grandchild).Count; i++)
                            {
                                string maQuyenHan = QHBO.LayMaTheoTen(DanhSachCheckCuaNode(grandchild)[i]);
                                ThemVaiTroChucNangConQuyenHan(maMaVaiTro, maChucNangCon, maQuyenHan);
                                //MessageBox.Show(maMaVaiTro + " " + maChucNangCon + " " + maQuyenHan);
                            }
                        }
                    }
                }
            }    
        }

        #endregion

        #region TreelistView

        TreeListNode lv1;
        TreeListNode lv2;
        TreeListNode lv3;
        public void KhoiTaoCay()
        {

            tlThemVaiTro.BeginUnboundLoad();

            for (int i = 0; i < NCNBO.DSTenNhomChucNang().Count(); i++)
            {
                lv1 = tlThemVaiTro.AppendNode(
                    new object[] { NCNBO.DSTenNhomChucNang()[i] }, null);
                
                string maNhom = NCNBO.LayMaTheoTen(NCNBO.DSTenNhomChucNang()[i]);
                for (int j = 0; j < CNBO.DSTenChucNangTheoNhom(maNhom).Count(); j++)
                {
                    lv2 = tlThemVaiTro.AppendNode(
                        new object[] { CNBO.DSTenChucNangTheoNhom(maNhom)[j] }, lv1);

                    string maChucNang = CNBO.LayMaTheoTen(CNBO.DSTenChucNangTheoNhom(maNhom)[j]);
                    for (int k = 0; k < CNCBO.DSChucNangConTheoChucNang(maChucNang).Count(); k++)
                    {
                        lv3 = tlThemVaiTro.AppendNode(
                            new object[] { CNCBO.DSChucNangConTheoChucNang(maChucNang)[k] }, lv2);
                    }
                }
            }

            tlThemVaiTro.EndUnboundLoad();
        }
        private void CheckTatCaChoMotCot(TreeListNode node, TreeListColumn col)
        {
            if (Convert.ToBoolean(node.GetValue(col)))
            {
                if(node.HasChildren)
                {
                    for (int i = 0; i < node.Nodes.Count; i++)
                    {
                        node.Nodes[i].SetValue(col, isCheck.ValueChecked);
                        CheckTatCaChoMotCot(node.Nodes[i], col);
                    }
                }
            }
            else if(node.HasChildren)
            {
                for (int i = 0; i < node.Nodes.Count; i++)
                {
                    CheckTatCaChoMotCot(node.Nodes[i], col);
                }
            }
        }

        private void KiemTraCheckCotChoCay()
        {
            for (int i = 0; i < tlThemVaiTro.Nodes.Count; i++)
            {
                CheckTatCaChoMotCot(tlThemVaiTro.Nodes[i], tlcolTruyCap);
                CheckTatCaChoMotCot(tlThemVaiTro.Nodes[i], tlcolThem);
                CheckTatCaChoMotCot(tlThemVaiTro.Nodes[i], tlcolXoa);
                CheckTatCaChoMotCot(tlThemVaiTro.Nodes[i], tlcolCapNhat);
                CheckTatCaChoMotCot(tlThemVaiTro.Nodes[i], tlcolIn);
                CheckTatCaChoMotCot(tlThemVaiTro.Nodes[i], tlcolNhap);
                CheckTatCaChoMotCot(tlThemVaiTro.Nodes[i], tlcolXuat);
            }
        }

        private void CheckTatCaChoMotNode(TreeListNode node)
        {
            node.SetValue(tlcolTatCa, isCheck.ValueChecked);
            node.SetValue(tlcolTruyCap, isCheck.ValueChecked);
            node.SetValue(tlcolThem, isCheck.ValueChecked);
            node.SetValue(tlcolXoa, isCheck.ValueChecked);
            node.SetValue(tlcolCapNhat, isCheck.ValueChecked);
            node.SetValue(tlcolIn, isCheck.ValueChecked);
            node.SetValue(tlcolNhap, isCheck.ValueChecked);
            node.SetValue(tlcolXuat, isCheck.ValueChecked);
        }
        private void UnCheckTatCaChoMotNode(TreeListNode node)
        {
            node.SetValue(tlcolTatCa, isCheck.ValueUnchecked);
            node.SetValue(tlcolTruyCap, isCheck.ValueUnchecked);
            node.SetValue(tlcolThem, isCheck.ValueUnchecked);
            node.SetValue(tlcolXoa, isCheck.ValueUnchecked);
            node.SetValue(tlcolCapNhat, isCheck.ValueUnchecked);
            node.SetValue(tlcolIn, isCheck.ValueUnchecked);
            node.SetValue(tlcolNhap, isCheck.ValueUnchecked);
            node.SetValue(tlcolXuat, isCheck.ValueUnchecked);
        }
        private void CheckTatCaCacNodeCon(TreeListNode node)
        {
            CheckTatCaChoMotNode(node);
            if (node.HasChildren)
            {
                for (int i = 0; i < node.Nodes.Count; i++)
                {
                    CheckTatCaChoMotNode(node.Nodes[i]);
                    if (node.Nodes[i].HasChildren)
                    {
                        CheckTatCaCacNodeCon(node.Nodes[i]);
                    }
                }
            }
        }

        private void UnCheckTatCaCacNodeCon(TreeListNode node)
        {
            if (node.HasChildren)
            {
                UnCheckTatCaChoMotNode(node);
                for (int i = 0; i < node.Nodes.Count; i++)
                {
                    UnCheckTatCaChoMotNode(node.Nodes[i]);
                    if (node.Nodes[i].HasChildren)
                    {
                        UnCheckTatCaCacNodeCon(node.Nodes[i]);
                    }
                }
            }
        }

        public void KiemTraCheckTatCa()
        {
            for (int i = 0; i < tlThemVaiTro.Nodes.Count; i++)
            {
                if (Convert.ToBoolean(tlThemVaiTro.Nodes[i].GetValue(tlcolTatCa)))
                {
                    CheckTatCaCacNodeCon(tlThemVaiTro.Nodes[i]);
                }
                else
                {
                    for (int j = 0; j < tlThemVaiTro.Nodes[i].Nodes.Count; j++)
                    {
                        if (Convert.ToBoolean(tlThemVaiTro.Nodes[i].Nodes[j].GetValue(tlcolTatCa)))
                        {
                            CheckTatCaCacNodeCon(tlThemVaiTro.Nodes[i].Nodes[j]);
                        }
                        else
                        {
                            for (int k = 0; k < tlThemVaiTro.Nodes[i].Nodes[j].Nodes.Count; k++)
                            {
                                if (Convert.ToBoolean(tlThemVaiTro.Nodes[i].Nodes[j].Nodes[k].GetValue(tlcolTatCa)))
                                {
                                    CheckTatCaCacNodeCon(tlThemVaiTro.Nodes[i].Nodes[j].Nodes[k]);
                                }
                            }
                        }
                    }
                }
            }
        }
        private void tlThemVaiTro_MouseUp(object sender, MouseEventArgs e)
        {
            KiemTraCheckTatCa();
            KiemTraCheckCotChoCay();
        }

        private void isCheck_EditValueChanged(object sender, EventArgs e)
        {
            KiemTraCheckTatCa();
            //isCheck.CheckedChanged -= isCheck_EditValueChanged;
        }

        private List<string> DanhSachCheckCuaNode(TreeListNode node)
        {
            List<string> DanhSachCheckCuaNode = new List<string>();

            if (Convert.ToBoolean(node.GetValue(tlcolTruyCap)))
                DanhSachCheckCuaNode.Add("Truy Xuất");
            if (Convert.ToBoolean(node.GetValue(tlcolThem)))
                DanhSachCheckCuaNode.Add("Thêm");
            if (Convert.ToBoolean(node.GetValue(tlcolXoa)))
                DanhSachCheckCuaNode.Add("Xóa");
            if (Convert.ToBoolean(node.GetValue(tlcolCapNhat)))
                DanhSachCheckCuaNode.Add("Sửa");
            if (Convert.ToBoolean(node.GetValue(tlcolIn)))
                DanhSachCheckCuaNode.Add("In");
            if (Convert.ToBoolean(node.GetValue(tlcolXuat)))
                DanhSachCheckCuaNode.Add("Xuất");
            if (Convert.ToBoolean(node.GetValue(tlcolNhap)))
                DanhSachCheckCuaNode.Add("Nhập");

            if (DanhSachCheckCuaNode != null)
                return DanhSachCheckCuaNode;
            else
                return null;
        }

        private string LayTextNode(TreeListNode node)
        {
            return node.GetDisplayText(tlcolChucNang);
        }

        #endregion

    }

}
